import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/home.vue'
import Sort from '../views/sort.vue'
import Cart from '../views/cart.vue'
import User from '../views/user.vue'
import Login from '../views/login.vue'
import Register from '../views/register.vue'
import GoodsClassify from '../views/goods-classify.vue'
import Details from '../views/details.vue'


Vue.use(VueRouter)

  const routes = [
	  {
	  		  path : '/',
	  		  redirect : '/home',
			  name : 'index'
	  },
	  {
		  path : '/home',
		  component : Home,
		  name : 'home'
	  },
	  {
		  path : '/sort',
		  component : Sort,
		  name : 'sort'
	  },
	  {
		  path : '/cart',
		  component :  Cart,
		  name : 'cart'
	  },
	  {
		  path : '/user',
		  component : User,
		  name : 'user'
	  },
	  {
	  	path : '/login',
	  	component : Login,
	  	name : 'login'
	  },
	 {
		path : '/register',
		component : Register,
		name : 'register'
	 },  
	 {
		 path : '/goods-classify',
		 component : GoodsClassify,
		 name : 'goods-classify'
	 },
	 {
		 path : '/details',
		 component : Details,
		 name : 'details'
	 }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


//路由守卫  路由拦截
//Vue-Router升级导致的Uncaught(in promise) navigation guard问题
//安装vue-router版本为3.0.7
router.beforeEach((to, from, next) => {
	
	//登录权限  localStorage
	let token = window.localStorage.getItem('token');
	//console.log(to.path); 
	
	//想去个人中心
	if((to.path === '/user' || to.path === '/cart') && !token){
		//这里只是设置路由
		return next('/login');
	};
	//这里才是真正的跳转
	next();
	
})
export default router
